<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\StoreCategory;
use App\StoreProduct;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ProductController extends Controller
{

    /**
     * @auther yang
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        $params = $request->input();
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $offset = page_limit($page,$limit);
        $merchant_id = $request->session()->get('admin.id');

        $where = [
            ['merchant_id','eq',$merchant_id],
        ];

        // 商品分类搜索
        if (!empty($params['cate_id'])) {
            $product_id = DB::table('eb_store_product_cate')->where('cate_id', $params['cate_id'])->pluck('product_id');
            $where[] = ['id', 'in', $product_id];
        }

        // 商品搜索
        if (!empty($params['store_name'])) {
            $where[] = ['store_name', 'like', $params['store_name']];
        }

        // 上下架
        $is_show = 1;
        if (isset($params['is_show'])) {
            $is_show = $params['is_show'];
        }
        $where[] = ['is_show', 'eq', $is_show];

        // 是否删除 仓库中的产品为未删除
        $is_del = 0;
        if (!empty($params['is_del'])) {
            $is_del = 1;
        }
        $where[] = ['is_del', 'eq', $is_del];

        // 已售罄 上架中, 属性库存为0 或商品库存为0

        $wd   = $request->input('wd');
        $db = DB::table('eb_store_product');
        $db = format_sql($db, $where);
        $list = $db->paginate(10);
        return view('admin.product_list', ['list' => $list, 'wd' => $wd]);
    }

    /**
     * 商品详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        $id = $request->input('id', 0);
        $data = StoreProduct::detail([['id', 'eq', $id]]);
        reply($data);
    }

    /**
     * @auther yang
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function editView(Request $request)
    {
        $id = $request->input('id', 0);
        $data = StoreProduct::detail([['id', 'eq', $id]]);

        $father = StoreCategory::getAll([['pid', 'eq', 0]]);
        foreach ($father as $key => $value) {
            $father[$key]['son'] = StoreCategory::getAll([['pid', 'eq', $value['id']]]);
        }

        return view('admin.product_edit', ['product' => $data, 'cate_list' => $father]);
    }

    /**
     * 修改
     * @auther yang
     * @param Request $request
     * @return array
     */
    public function edit(Request $request)
    {
        $params = $request->post();
        if (empty($params['bar_code'])) $params['bar_code'] = '';
        $result = crmeb_curl(6, $params);
        return $result;
    }

    /**
     * @auther yang
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function addView()
    {
        $father = StoreCategory::getAll([['pid', 'eq', 0]]);
        foreach ($father as $key => $value) {
            $father[$key]['son'] = StoreCategory::getAll([['pid', 'eq', $value['id']]]);
        }
        // 分类
        return view('admin.product_add', ['cate_list'=>$father]);
    }

    /**
     * 创建
     * @auther yang
     * @param Request $request
     * @return array
     */
    public function add(Request $request)
    {
        $params = $request->post();
        $merchant_id = $request->session()->get('admin.id');
        $params['merchant_id'] = $merchant_id;
        if (empty($params['bar_code'])) $params['bar_code'] = '';
        $result = crmeb_curl(5, $params);
        return $result;
    }

    /**
     * 删除
     * @auther yang
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $id = $request->input('id');

        if (empty($id)) {
            reply_error('参数缺失');
        } else {
            $product = StoreProduct::detail([['id','eq',$id]]);
        }

        if (!empty($product)) {
            StoreProduct::edit([['id','eq',$id]], ['is_del' => 1]);
        }

        reply([],'删除成功');
    }
}
